#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"

void QInit(Queue* q)
{
	assert(q);
	q->phead = q->ptail = 0;
	q->size = 0;
}

void QDesTory(Queue* q)
{
	assert(q);
	QueueNode* pcur = q->phead;
	while (pcur)
	{
		QueueNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	q->phead = q->ptail = NULL;
	q->size = 0;
}

void QPush(Queue* q, QDataType s)
{
	assert(q);
	QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newNode == NULL)
	{
		perror("malloc");
		exit(1);
	}
	newNode->next = NULL;
	newNode->data = s;
	if (q->phead == NULL)
	{
		q->phead = q->ptail = newNode;
	}
	else
	{
		q->ptail->next = newNode;
		q->ptail = q->ptail->next;
	}
	q->size++;
}

bool Empty(Queue* q)
{
	assert(q);
	return q->phead == NULL;
}
void QPop(Queue* q)
{
	assert(!Empty(&q));
	if (q->phead == q->ptail)
	{
		free(q->phead);
		q->phead = q->ptail = NULL;
	}
	else
	{
		QueueNode* next = q->phead->next;
		free(q->phead);
		q->phead = next;
	}
	q->size--;
}

QDataType QFront(Queue* q)
{
	assert(!Empty(q));
	return q->phead->data;
}

QDataType QBack(Queue* q)
{
	assert(!Empty(q));
	return q->ptail->data;
}

QDataType QSize(Queue* q)
{
	return q->size;
}